package com.koron.device.config;

import com.koron.bean.system.staff.Constant;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName SwaggerConfig
 * @description
 * @Author zhouj
 * @Date 2021/5/8 8:48
 */

@Configuration
public class SwaggerConfig {

    @Value("${spring.profiles.active}")
    private String profile;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(StringUtils.equalsAny(profile, "dev", "test"))
                .groupName("设备台账管理")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .description("设备台账管理")
                .version("2.0")
                .build();

    }

    private List<SecurityScheme> securitySchemes() {
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey(Constant.JWT_TOKEN, Constant.JWT_TOKEN, "header"));
        apiKeyList.add(new ApiKey(Constant.DYNAMIC_DATA_SOURCE, Constant.DYNAMIC_DATA_SOURCE, "header"));
        apiKeyList.add(new ApiKey(Constant.LANGUAGE, Constant.LANGUAGE, "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .build()
        );
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        List<SecurityReference> securityReferenceList = new ArrayList<>();
        securityReferenceList.add(new SecurityReference(Constant.JWT_TOKEN, new AuthorizationScope[]{authorizationScope}));
        securityReferenceList.add(new SecurityReference(Constant.DYNAMIC_DATA_SOURCE, new AuthorizationScope[]{authorizationScope}));
        securityReferenceList.add(new SecurityReference(Constant.LANGUAGE, new AuthorizationScope[]{authorizationScope}));
//        securityReferenceList.add(new SecurityReference(Constant.LANGUAGE, new AuthorizationScope[]{authorizationScope}));
        return securityReferenceList;
    }

}
